package com.flymobi.service;

import java.util.List;

import com.flymobi.fw.modules.CommonService;
import com.flymobi.po.PageContent;
import com.flymobi.util.QL;

public class PageContentService extends CommonService {

	public List<PageContent> list(Long page, int startRow,
			int pagePerSize) {
		return dao.find(PageContent.class, QL.HQL.PageContent.LIST_BY_PAGE, new Object[] {
			page }, startRow, pagePerSize);
	}

	public void save(PageContent PageContent) {
		dao.save(PageContent);
	}

	public PageContent load(String id) {
		return dao.load(PageContent.class, new Long(id));
	}

	public PageContent get(String id) {
		return dao.get(PageContent.class, new Long(id));
	}

	public void delete(String id) {
		dao.delete(this.get(id));
	}

	public void updateUp(String id) {
		PageContent pe = this.get(id);
		PageContent peUp = findUp(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}

	public void updateDown(String id) {
		PageContent pe = this.get(id);
		PageContent peUp = findDown(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}

	private void switchPosion(PageContent pe, PageContent peUp) {
		long pe1idx = pe.getIdx();
		pe.setIdx(peUp.getIdx());
		peUp.setIdx(pe1idx);
		dao.save(pe);
		dao.save(peUp);
	}

	private PageContent findUp(PageContent pe) {
		return dao.findSingle(PageContent.class, QL.HQL.PageContent.LIST_UP,
				new Object[] { pe.getPage(), pe.getIdx() });
	}

	private PageContent findDown(PageContent pe) {
		return dao.findSingle(PageContent.class, QL.HQL.PageContent.LIST_DOWN,
				new Object[] { pe.getPage(), pe.getIdx() });
	}
}
